<?php

namespace app\common\service;

use app\common\job\Sms;
use app\common\job\TemplateSms;
use think\facade\Config;
use think\facade\Queue;

/**
 * 模板短信
 * Class TemplateSmsService
 * @package app\common\service
 */
class TemplateSmsService
{
    /**
     * 抢单提醒
     * @param $phone
     * @return void
     */
    public function rushRemind($phone)
    {

        $template_id = 'SMS_462295129';

        $params = [];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 发起提现后，发短信给商户平台管理员，提醒审核
     * @return void
     */
    public function cashRemind()
    {
        $phone = '19183958786';

        $template_id = 'SMS_462050118';

        $params = [];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 律师资料审核结果
     * @param $phone
     * @param $result
     * @return void
     */
    public function lawyerCheckResult($phone, $result)
    {
        $template_id = 'SMS_264925548';

        $params = [
            'result' => $result,
        ];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 法务资料审核结果
     * @param $phone
     * @param $result
     * @return void
     */
    public function legalCheckResult($phone, $result)
    {
        $template_id = 'SMS_463599966';

        $params = [
            'result' => $result,
        ];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 提现转账结果
     * @param $phone
     * @param $money
     * @param $time
     * @return void
     */
    public function cashTransferResult($phone, $money, $time)
    {

        $template_id = 'SMS_264800569';

        $params = [
            'date' => date('Y-m-d H:i', $time),
            'money' => $money,
        ];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 用户付费
     * @param $phone
     * @param $name
     * @param $money
     * @return void
     */
    public function memberPay($phone, $name, $money)
    {

        $template_id = 'SMS_264885521';

        $params = [
            'name' => $name,
            'money' => $money,
        ];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 律师首次回复
     * @param $phone
     * @return void
     */
    public function lawyerReply($phone)
    {
        $template_id = 'SMS_255390486';

        $params = [];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 用户触发服务卡片后未付款
     * @param $phone
     * @param $consultingId
     * @return void
     */
    public function memberUnPay($phone, $consultingId)
    {

        $content = '【熊猫律师】您刚提交的问题律师团队已评估完成，点击支付 m.xiongmaolvshi.cn{$var} 获取金牌律师团队1对1解答';

        $var = ['/sms1/' . $consultingId];

        $this->pushMessage2($phone, $content, $var);
    }

    /**
     * 律师首次回复 v2
     * @param $phone
     * @param $ask
     * @param $lawyerName
     * @param $consultingId
     * @return void
     */
    public function lawyerReply2($phone, $ask, $lawyerName, $consultingId)
    {
        if (mb_strlen($ask) > 11) {
            $ask = mb_substr($ask, 0, 10) . '…';
        }

        $content = '【熊猫律师】您发布的免费咨询“{$var}”，{$var}律师已回复，点击 m.xiongmaolvshi.cn/sms2 去熊猫律师小程序查看律师解答并追问，回TD退订';

        $var = [$ask, $lawyerName];

        $this->pushMessage2($phone, $content, $var);
    }

    /**
     * 用户创建落地页订单后未支付
     * @param $phone
     * @param $tradeId
     * @return void
     */
    public function memberLandingUnPay($phone, $tradeId)
    {

        $content = '【熊猫律师】您刚提交的问题律师团队已评估完成，点击支付 m.xiongmaolvshi.cn{$var} 获取金牌律师团队1对1解答';

        $var = ['/sms3/' . $tradeId];

        $this->pushMessage2($phone, $content, $var);
    }

    /**
     * 微信商户平台资金不足（提现失败和退款失败时提醒）
     * @return void
     */
    public function wechatMoneyNotEnough()
    {
        $phone = '19183958786';

        $template_id = 'SMS_248430313';

        $params = [];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 公司邀请绑定员工
     * @param $phone
     * @param $companyName
     * @param $userPersonnelId
     * @return void
     */
    public function invitePersonnel($phone, $companyName, $userPersonnelId)
    {
        // 您好，${company}已添加您为员工，同意请点击 m.xiongmaolvshi.cn/yg/${code} 立即绑定，不同意可忽略
        $template_id = 'SMS_460960081';

        $params = [
            'company' => $companyName,
            'code' => $userPersonnelId
        ];

        $this->pushMessage($phone, $template_id, $params);
    }

    /**
     * 提醒用户付费1
     * @param $phone
     * @return void
     */
    public function remindPay1($phone, $gid)
    {

        $content = '【熊猫律师】您的问题已有律师接单，点 m.xiongmaolvshi.cn{$var} 支付后将有金牌律师为您提供1对1服务，不满意可退款';

        $var = ['/s/' . $gid];

        $this->pushMessage2($phone, $content, $var);
    }

    /**
     * 提醒用户付费2
     * @param $phone
     * @return void
     */
    public function remindPay2($phone, $gid)
    {
        $content = '【熊猫律师】您咨询的法律问题，已匹配到律师服务，点击 m.xiongmaolvshi.cn{$var} 查看详情';

        $var = ['/s/' . $gid];

        $this->pushMessage2($phone, $content, $var);
    }

    private function pushMessage($phone, $template_id, $params)
    {
        $job_data = [
            'template_id' => $template_id,
            'sign_name' => Config::get('sms_template.sign_name', ''),
            'params' => $params,
            'phone' => $phone,
        ];

        Queue::push(TemplateSms::class, $job_data);
    }

    private function pushMessage2(string $phone, $content, array $var)
    {
        $job_data = [
            'phone' => $phone,
            'content' => $content,
            'var' => $var,
        ];

        Queue::push(Sms::class, $job_data);
    }
}